package game.gui;

import engine.Engine;
import engine.game.Manager;
import engine.gui.GridField;
import engine.gui.Panel;
import game.objects.nonstaticObjects.EntityModel;
import game.objects.nonstaticObjects.ItemModel;

public class Inventory extends Panel {

	private static final long serialVersionUID = 1L;

	private static final int WIDTH = 400, HEIGHT = 400 ; 
	
	private InventoryGrid grid ; 
	private final EntityModel entity ; 
	
	public Inventory(Manager m, EntityModel entity) {
		super(m, (Engine.WIDTH - WIDTH), (Engine.HEIGHT - HEIGHT), WIDTH, HEIGHT);
		this.entity = entity ; 
		init();
	}
	
	public void init(){
		super.init(); 
		grid = new InventoryGrid(this.getManager(), this, this.getObjectWidth(), 
					this.getObjectWidth()); 
		this.addNewChild("grid",grid);
		this.enableBorder(); 
		this.hide(); 
	}
	
	public boolean hasFreeSlot(){
		return this.grid.hasEmptyBox(); 
	}
	
	public GridField getFirstFreeSlot(){
		return this.grid.getFirstEmptyBox();
	}
	
	public void addNewItem(ItemModel item){
		this.grid.addNewItem(item); 
	}

	public void update(){
		super.update(); 
		if(this.entity.hasPickedUpNewItem() && entity.getIDOfFirstFreeInventorySlot() != -1){
			this.addNewItem(entity.getLatetstPickedUp()); 
		}
		for(GridField[] f1: this.grid.getBoxes()){
			for(GridField f2: f1){
				if(f2.hasJustRemovedContent()){
					System.out.println(f2.getID());
					this.entity.dropItem(f2.getID()); 
				}
			}
		}
	}
	
}
